<template>
<div :class="viewFlag === 'formDesign' ? 'maskDiv' : ''">
    <div class="picDivCss">
        <div v-for="(item, idx) in newOption" :key="idx" class="everyPic">
            <img :src="item.value" style="height: 180px;width: 180px;border-radius: 5px;"/>
            <el-radio v-if="!multiple" v-model="radio" :label="item.id" @change="e => checkChange(e, idx)">{{item.label}}</el-radio>
            <el-checkbox v-if="timeStamp && multiple" :label="item.value" v-model="item.checked" @change="e => checkChange(e, idx)" :title="item.label">{{item.label}}</el-checkbox>
        </div>
    </div>
</div>
</template>
<script>
import { deepClone } from '../../utils'
export default {
    name: 'pictureSelect',
    props: [
      'tableName',
      'options',
      'value',
      'multiple',
      'viewFlag'
    ],
    data () {
        return {
            newOption: [],
            timeStamp: Date.now(),
            radio: '',
        }
    },
    watch: {
        options (newVal, oldVal) {
            this.newOption = deepClone(newVal)
        },
        value (newVal, oldVal) {
            if (newVal) {
                this.setValue(newVal)
            } else {
                if (this.multiple) {
                    this.newOption.forEach(cur => {
                        this.$set(cur, 'checked', false)
                    })
                } else {
                    this.radio = ''
                }
            }
        },
        multiple (newVal, oldVal) {
            this.$emit('change', '')
            if (newVal) {
                this.newOption.forEach(cur => {
                    this.$set(cur, 'checked', false)
                })
            } else {
                this.radio = ''
            }
        }
    },
    created () {
        this.newOption = deepClone(this.options)
        if (this.value) {
            this.setValue(this.value)
        }
    },
    methods: {
        setValue (value) {
            const idList = value.split(',')
            this.newOption.forEach(cur => {
                if (this.multiple) {
                    if (idList.includes(cur.id)) {
                        this.$set(cur, 'checked', true)
                    } else {
                        this.$set(cur, 'checked', false)
                    }
                } else {
                    this.radio = value
                }
            })
        },
        checkChange (e, row) {
            this.$set(this.newOption[row], 'checked', e)
            const tempList = []
            this.newOption.forEach(pic => {
                if (pic.checked) {
                    tempList.push(pic.id)
                }
            })
            this.$emit('change', tempList.join(','))
            this.timeStamp = Date.now()
        }
    }
}
</script>
<style lang="less" scoped>
.maskDiv{
    background-color: white;
    opacity: 1;
    pointer-events: none;
}
.picDivCss{
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    width: 100%;
    height: 100%;
    background: lightgrey;
}
.everyPic {
    width: 200px;
    border: 1px solid lightgray;
    float: left;
    background-color: white;
    padding: 10px;
    margin: 10px 5px;
    border-radius: 5px;
}
/deep/.el-checkbox__label{
    width: 150px;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 12px;
}
</style>